package com.dxq.test.db;

import android.content.Context;
import android.util.Log;

import com.alibaba.fastjson.JSONObject;
import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.JsonUtils;
import com.blankj.utilcode.util.LogUtils;
import com.google.gson.reflect.TypeToken;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.dao.GenericRawResults;
import com.j256.ormlite.stmt.PreparedQuery;
import com.j256.ormlite.stmt.QueryBuilder;
import com.j256.ormlite.stmt.SelectArg;

import java.sql.SQLException;
import java.util.List;
import java.util.concurrent.Callable;

/**
 * Author: zsh
 * Date: 2021/9/8
 * Describe: LabelDao
 */
public class TablesDao implements TablesImpl {

    private Context context;
    //通过此集合和DatabaseHelper的Map集合相对应  Dao中的类 如User 就可以随意变换了
    private Dao<Label, Integer> labelDao;
    private Dao<Event, Integer> eventDao;
    private Dao<Pasture, Integer> pastureDao;
    private Dao<Group, Integer> groupDao;
    private Dao<Vaccine, Integer> vaccineDao;
    private Dao<Breed, Integer> breedDao;
    private Dao<Food, Integer> foodDao;
    private Dao<Feed, Integer> feedDao;
    private Dao<LabelGroup, Integer> labelGroupDao;
    private Dao<LabelEvent, Integer> labelEventDao;
    private Dao<LabelVaccine, Integer> labelVaccineDao;
    private Dao<GroupVaccine, Integer> groupVaccineDao;
    private Dao<LabelBreed, Integer> labelBreedDao;
    private Dao<FeedGroupFood, Integer> feedGroupFoodDao;
    private DatabaseHelper helper;

    public TablesDao(Context context) {
        this.context = context;
        try {
            helper = DatabaseHelper.getHelper(context);
            labelDao = helper.getDao(Label.class);
            eventDao = helper.getDao(Event.class);
            pastureDao = helper.getDao(Pasture.class);
            groupDao = helper.getDao(Group.class);
            vaccineDao = helper.getDao(Vaccine.class);
            breedDao = helper.getDao(Breed.class);
            foodDao = helper.getDao(Food.class);
            feedDao = helper.getDao(Feed.class);
            labelGroupDao = helper.getDao(LabelGroup.class);
            labelEventDao = helper.getDao(LabelEvent.class);
            labelVaccineDao = helper.getDao(LabelVaccine.class);
            groupVaccineDao = helper.getDao(GroupVaccine.class);
            labelBreedDao = helper.getDao(LabelBreed.class);
            feedGroupFoodDao = helper.getDao(FeedGroupFood.class);
        } catch (SQLException e) {
            e.printStackTrace();
            LogUtils.e(e.getMessage());
        }
    }

    //新增
    @Override
    public int insertLabel(Label label) {
        try {
            return labelDao.create(label);
        } catch (SQLException e) {
            e.printStackTrace();
            LogUtils.e(e.getMessage());
        }
        return 0;
    }

    //批量新增
    @Override
    public int insertLabel(List<Label> list) {
        try {
            return labelDao.create(list);
        } catch (SQLException e) {
            e.printStackTrace();
            LogUtils.e(e.getMessage());
        }
        return 0;
    }

    //删除
    @Override
    public int deleteLabel(Label label) {
        try {
            return labelDao.delete(label);
        } catch (SQLException e) {
            e.printStackTrace();
            LogUtils.e(e.getMessage());
        }
        return 0;
    }

    //批量删除
    @Override
    public int deleteLabelBatch(List<Label> labels) {
        try {
            labelDao.callBatchTasks(new Callable<Void>() {
                @Override
                public Void call() throws Exception {
                    for (Label label : labels) {
                        deleteLabel(label);
                    }
                    return null;
                }
            });
            return 0;
        } catch (SQLException e) {
            e.printStackTrace();
            LogUtils.e(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    //删除全部
    @Override
    public int deleteLabelAll() {
        try {
            String sql = "delete from Label";
            GenericRawResults<String[]> delete_sql = labelDao.queryRaw(sql);
            return delete_sql.getResults().size();
        } catch (SQLException e) {
            e.printStackTrace();
            LogUtils.e(e.getMessage());
        }
        return 0;
    }

    //更新
    @Override
    public int updateLabel(Label label) {
        try {
            return labelDao.update(label);
        } catch (SQLException e) {
            e.printStackTrace();
            LogUtils.e(e.getMessage());
        }
        return 0;
    }

    //批量更新
    @Override
    public int updateLabelBatch(List<Label> labels) {
        return 0;
    }

    //查询所有
    @Override
    public List<Label> queryLabelAll() {
        try {
            return labelDao.queryForAll();
        } catch (SQLException e) {
            e.printStackTrace();
            LogUtils.e(e.getMessage());
        }
        return null;
    }

    //根据id查询
    @Override
    public Label queryLabelById(int id) {
        try {
            return labelDao.queryForId(id);
        } catch (SQLException e) {
            e.printStackTrace();
            LogUtils.e(e.getMessage());
        }
        return null;
    }

//    //牲畜查询
//    public List<Label> queryLabels(String sql) {
//        try {
//            return labelDao.queryRaw(sql).getResults();
////            LogUtils.e(results);
////            String json = GsonUtils.toJson(results.getResults());
////            return GsonUtils.fromJson(json, new TypeToken<List<Label>>(){}.getType());
//        } catch (SQLException e) {
//            e.printStackTrace();
//            LogUtils.e(e.getMessage());
//        }
//        return null;
//    }

    //牲畜查询
    public List<Label> queryLabels(int pasture_id, String breedingStatus, boolean isSerialNumber) {
        try {
            if (isSerialNumber)
                return labelDao.queryBuilder()
                        .where().eq("pasture_id", pasture_id)
                        .and().eq("breedingStatus", breedingStatus)
                        .and().ne("serialNumber", "")//不等于
                        .query();
            return labelDao.queryBuilder()
                    .where().eq("pasture_id", pasture_id)
                    .and().eq("breedingStatus", breedingStatus)
                    .query();
        } catch (SQLException e) {
            e.printStackTrace();
            LogUtils.e(e.getMessage());
        }
        return null;
    }

    //新增--事件
    @Override
    public int insertEvent(Event event) {
        try {
            return eventDao.create(event);
        } catch (SQLException e) {
            e.printStackTrace();
            LogUtils.e(e.getMessage());
        }
        return 0;
    }

    //更新--事件
    public int updateEvent(Event event) {
        try {
            return eventDao.update(event);
        } catch (SQLException e) {
            e.printStackTrace();
            LogUtils.e(e.getMessage());
        }
        return 0;
    }

    //删除--事件
    public int deleteEvent(Event event) {
        try {
            return eventDao.delete(event);
        } catch (SQLException e) {
            e.printStackTrace();
            LogUtils.e(e.getMessage());
        }
        return 0;
    }

    //查询--事件（根据牲畜查询对应的事件）
    public List<Event> queryEventByLabel(Label label) {
        try {
            return eventDao.queryBuilder()
                    .query();
        } catch (SQLException e) {
            e.printStackTrace();
            LogUtils.e(e.getMessage());
        }
        return null;
    }

    //查询所有event
    @Override
    public List<Event> queryEventAll() {
        try {
            return eventDao.queryForAll();
        } catch (SQLException e) {
            e.printStackTrace();
            LogUtils.e(e.getMessage());
        }
        return null;
    }

    //根据id查询
    @Override
    public Event queryEventById(int id) {
        try {
            return eventDao.queryForId(id);
        } catch (SQLException e) {
            e.printStackTrace();
            LogUtils.e(e.getMessage());
        }
        return null;
    }

    @Override
    public int insertPasture(Pasture pasture) {
        try {
            List<Pasture> pastures = pastureDao.queryForAll();
            if (null != pastures && pastures.size() > 0) {
                for (Pasture pasture1 : pastures) {
                    if (pasture1.getName().equals(pasture.getName())) {
                        return 0;
                    }
                }
            }
            return pastureDao.create(pasture);
        } catch (SQLException e) {
            e.printStackTrace();
            LogUtils.e(e.getMessage());
        }
        return 0;
    }

    @Override
    public List<Pasture> queryPastureAll() {
        try {
            return pastureDao.queryForAll();
        } catch (SQLException e) {
            e.printStackTrace();
            LogUtils.e(e.getMessage());
        }
        return null;
    }

    @Override
    public Pasture queryPastureById(int id) {
        try {
            return pastureDao.queryForId(id);
        } catch (SQLException e) {
            e.printStackTrace();
            LogUtils.e(e.getMessage());
        }
        return null;
    }

    @Override
    public List<Pasture> queryPastureByName(String name) {
        try {
            return pastureDao.queryBuilder().where().eq("name", name).query();
        } catch (SQLException e) {
            e.printStackTrace();
            LogUtils.e(e.getMessage());
        }
        return null;
    }

    public void leftJoinQuery(int id, int eventID) {
        try {
            QueryBuilder<Label, Integer> labelBuilder = labelDao.queryBuilder();
            QueryBuilder<Event, Integer> eventBuilder = eventDao.queryBuilder();

            eventBuilder.where().eq("id", eventID);
            labelBuilder.leftJoin(eventBuilder);
            labelBuilder.where().eq("ids", id);
            List<Label> query = labelBuilder.query();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    //新增--群
    public int insertGroup(Group group) {
        try {
            return groupDao.create(group);
        } catch (SQLException e) {
            e.printStackTrace();
            LogUtils.e(e.getMessage());
        }
        return 0;
    }

    //删除--群
    public int deleteGroup(Group group) {
        try {
            return groupDao.delete(group);
        } catch (SQLException e) {
            e.printStackTrace();
            LogUtils.e(e.getMessage());
        }
        return 0;
    }

    //更新--群
    public int updateGroup(Group group) {
        try {
            return groupDao.update(group);
        } catch (SQLException e) {
            e.printStackTrace();
            LogUtils.e(e.getMessage());
        }
        return 0;
    }

    //查询--根据label查询group
    public List<Group> queryGroupForLabel(Label label) {
        PreparedQuery<Group> groupForLabelQuery = null;
        try {
            if (groupForLabelQuery == null) {
                groupForLabelQuery = makeGroupForLabelQuery();
            }
            groupForLabelQuery.setArgumentHolderValue(0, label);
            return groupDao.query(groupForLabelQuery);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public PreparedQuery<Group> makeGroupForLabelQuery() {
        try {
            // build our inner query for UserPost objects
            QueryBuilder<LabelGroup, Integer> userPostQb = labelGroupDao.queryBuilder();
            // just select the post-id field
            userPostQb.selectColumns(LabelGroup.GROUP_ID_FIELD_NAME);
            SelectArg userSelectArg = new SelectArg();
            // you could also just pass in user1 here
            userPostQb.where().eq(LabelGroup.LABEL_ID_FIELD_NAME, userSelectArg);

            // build our outer query for Post objects
            QueryBuilder<Group, Integer> postQb = groupDao.queryBuilder();
            // where the id matches in the post-id from the inner query
            postQb.where().in(Group.ID_FIELD_NAME, userPostQb);
            return postQb.prepare();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    //查询--根据group查询label
    public List<Label> queryLabelForGroup(Group group) {
        PreparedQuery<Label> labelForGroupQuery = null;
        try {
            if (labelForGroupQuery == null) {
                labelForGroupQuery = makeLabelForGroupQuery();
                System.out.println(labelForGroupQuery.toString());
            }
            labelForGroupQuery.setArgumentHolderValue(0, group);
            return labelDao.query(labelForGroupQuery);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public PreparedQuery<Label> makeLabelForGroupQuery() {
        try {
            QueryBuilder<LabelGroup, Integer> userPostQb = labelGroupDao.queryBuilder();
            // this time selecting for the user-id field
            userPostQb.selectColumns(LabelGroup.LABEL_ID_FIELD_NAME);
            SelectArg postSelectArg = new SelectArg();
            userPostQb.where().eq(LabelGroup.GROUP_ID_FIELD_NAME, postSelectArg);

            // build our outer query
            QueryBuilder<Label, Integer> userQb = labelDao.queryBuilder();
            // where the user-id matches the inner query's user-id field
            userQb.where().in(Label.ID_FIELD_NAME, userPostQb);
            return userQb.prepare();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    //查询--群
    public List<Group> queryGroupAll() {
        try {
            return groupDao.queryForAll();
        } catch (SQLException e) {
            e.printStackTrace();
            LogUtils.e(e.getMessage());
        }
        return null;
    }

    //查询--群
    public Group queryGroupById(int id) {
        try {
            return groupDao.queryForId(id);
        } catch (SQLException e) {
            e.printStackTrace();
            LogUtils.e(e.getMessage());
        }
        return null;
    }

    //查询--根据群名称查询
    public List<Group> queryGroupByName(String groupName) {
        try {
            return groupDao.queryBuilder()
                    .where().eq("name", groupName)
                    .query();
        } catch (SQLException e) {
            e.printStackTrace();
            LogUtils.e(e.getMessage());
        }
        return null;
    }

    //新增--牲畜，群
    public int insertLabelGroup(LabelGroup labelGroup) {
        try {
            return labelGroupDao.create(labelGroup);
        } catch (SQLException e) {
            e.printStackTrace();
            LogUtils.e(e.getMessage());
        }
        return 0;
    }

    //新增--牲畜，群
    public int insertLabelGroup(List<LabelGroup> labelGroupList) {
        try {
            return labelGroupDao.create(labelGroupList);
        } catch (SQLException e) {
            e.printStackTrace();
            LogUtils.e(e.getMessage());
        }
        return 0;
    }

    //删除--牲畜，群
    public int deleteLabelGroup(LabelGroup labelGroup) {
        try {
            return labelGroupDao.delete(labelGroup);
        } catch (SQLException e) {
            e.printStackTrace();
            LogUtils.e(e.getMessage());
        }
        return 0;
    }

    //批量删除--牲畜，群
    public int deleteLabelGroup(List<LabelGroup> list) {
        try {
            labelGroupDao.callBatchTasks(new Callable<Void>() {
                @Override
                public Void call() throws Exception {
                    for (LabelGroup labelGroup : list) {
                        labelGroupDao.delete(labelGroup);
                    }
                    return null;
                }
            });
            return 0;
        } catch (SQLException e) {
            e.printStackTrace();
            LogUtils.e(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    //根据label和group查询LabelGroup
    public List<LabelGroup> queryLabelGroupByLabelAndGroup(Label label, Group group) {
        try {
            int label_id = label.getLabel_id();
            int group_id = group.getGroup_id();
            return labelGroupDao.queryBuilder()
                    .where().eq(LabelGroup.LABEL_ID_FIELD_NAME, label_id)
                    .and().eq(LabelGroup.GROUP_ID_FIELD_NAME, group_id)
                    .query();
        } catch (SQLException e) {
            e.printStackTrace();
            LogUtils.e(e.getMessage());
        }
        return null;
    }

    //根据group查询LabelGroup
    public List<LabelGroup> queryLabelGroupByGroup(Group group) {
        try {
            int group_id = group.getGroup_id();
            return labelGroupDao.queryBuilder()
                    .where().eq(LabelGroup.GROUP_ID_FIELD_NAME, group_id)
                    .query();
        } catch (SQLException e) {
            e.printStackTrace();
            LogUtils.e(e.getMessage());
        }
        return null;
    }

////////////////////////////////////////////////////////////////////////////////////////////////////


    //新增--牲畜，事件
    public int insertLabelEvent(LabelEvent labelEvent) {
        try {
            return labelEventDao.create(labelEvent);
        } catch (SQLException e) {
            e.printStackTrace();
            LogUtils.e(e.getMessage());
        }
        return 0;
    }

    //新增--牲畜，事件
    public int insertLabelEvent(List<LabelEvent> labelEventList) {
        try {
            return labelEventDao.create(labelEventList);
        } catch (SQLException e) {
            e.printStackTrace();
            LogUtils.e(e.getMessage());
        }
        return 0;
    }

    //删除--牲畜，事件
    public int deleteLabelEvent(LabelEvent labelEvent) {
        try {
            return labelEventDao.delete(labelEvent);
        } catch (SQLException e) {
            e.printStackTrace();
            LogUtils.e(e.getMessage());
        }
        return 0;
    }

    //批量删除--牲畜，事件
    public int deleteLabelEvent(List<LabelEvent> list) {
        try {
            labelGroupDao.callBatchTasks(new Callable<Void>() {
                @Override
                public Void call() throws Exception {
                    for (LabelEvent labelEvent : list) {
                        labelEventDao.delete(labelEvent);
                    }
                    return null;
                }
            });
            return 0;
        } catch (SQLException e) {
            e.printStackTrace();
            LogUtils.e(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    //查询--根据label查询event
    public List<Event> queryEventForLabel(Label label) {
        PreparedQuery<Event> eventForLabelQuery = null;
        try {
            if (eventForLabelQuery == null) {
                eventForLabelQuery = makeEventForLabelQuery();
            }
            eventForLabelQuery.setArgumentHolderValue(0, label);
            return eventDao.query(eventForLabelQuery);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public PreparedQuery<Event> makeEventForLabelQuery() {
        try {
            QueryBuilder<LabelEvent, Integer> userPostQb = labelEventDao.queryBuilder();
            userPostQb.selectColumns(LabelEvent.EVENT_ID_FIELD_NAME);
            SelectArg userSelectArg = new SelectArg();
            userPostQb.where().eq(LabelEvent.LABEL_ID_FIELD_NAME, userSelectArg);
            QueryBuilder<Event, Integer> postQb = eventDao.queryBuilder();
            postQb.where().in(Event.ID_FIELD_NAME, userPostQb);
            return postQb.prepare();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    //根据event查询LabelEvent
    public List<LabelEvent> queryLabelEventByEvent(Event event) {
        try {
            int event_id = event.getEvent_id();
            return labelEventDao.queryBuilder()
                    .where().eq(LabelEvent.EVENT_ID_FIELD_NAME, event_id)
                    .query();
        } catch (SQLException e) {
            e.printStackTrace();
            LogUtils.e(e.getMessage());
        }
        return null;
    }

////////////////////////////////////////////////////////////////////////////////////////////////////

    //新增--疫苗
    public int insertVaccine(Vaccine vaccine) {
        try {
            return vaccineDao.create(vaccine);
        } catch (SQLException e) {
            e.printStackTrace();
            LogUtils.e(e.getMessage());
        }
        return 0;
    }

    //删除--疫苗
    public int deleteVaccine(Vaccine vaccine) {
        try {
            return vaccineDao.delete(vaccine);
        } catch (SQLException e) {
            e.printStackTrace();
            LogUtils.e(e.getMessage());
        }
        return 0;
    }

    //更新--疫苗
    public int updateVaccine(Vaccine vaccine) {
        try {
            return vaccineDao.update(vaccine);
        } catch (SQLException e) {
            e.printStackTrace();
            LogUtils.e(e.getMessage());
        }
        return 0;
    }

    //查询--疫苗
    public List<Vaccine> queryVaccineAll() {
        try {
            return vaccineDao.queryForAll();
        } catch (SQLException e) {
            e.printStackTrace();
            LogUtils.e(e.getMessage());
        }
        return null;
    }

    //查询--疫苗
    public Vaccine queryVaccineById(int id) {
        try {
            return vaccineDao.queryForId(id);
        } catch (SQLException e) {
            e.printStackTrace();
            LogUtils.e(e.getMessage());
        }
        return null;
    }

    //查询--疫苗--根据名称查询
    public List<Vaccine> queryVaccineByName(String groupName) {
        try {
            return vaccineDao.queryBuilder()
                    .where().eq("name", groupName)
                    .query();
        } catch (SQLException e) {
            e.printStackTrace();
            LogUtils.e(e.getMessage());
        }
        return null;
    }

    //新增--牲畜，疫苗
    public int insertLabelVaccine(LabelVaccine labelVaccine) {
        try {
            return labelVaccineDao.create(labelVaccine);
        } catch (SQLException e) {
            e.printStackTrace();
            LogUtils.e(e.getMessage());
        }
        return 0;
    }

    //新增--牲畜，疫苗
    public int insertLabelVaccine(List<LabelVaccine> list) {
        try {
            return labelVaccineDao.create(list);
        } catch (SQLException e) {
            e.printStackTrace();
            LogUtils.e(e.getMessage());
        }
        return 0;
    }

    //删除--牲畜，疫苗
    public int deleteLabelVaccine(LabelVaccine labelVaccine) {
        try {
            return labelVaccineDao.delete(labelVaccine);
        } catch (SQLException e) {
            e.printStackTrace();
            LogUtils.e(e.getMessage());
        }
        return 0;
    }

    //批量删除--牲畜，疫苗
    public int deleteLabelVaccine(List<LabelVaccine> list) {
        try {
            labelGroupDao.callBatchTasks(new Callable<Void>() {
                @Override
                public Void call() throws Exception {
                    for (LabelVaccine labelVaccine : list) {
                        labelVaccineDao.delete(labelVaccine);
                    }
                    return null;
                }
            });
            return 0;
        } catch (SQLException e) {
            e.printStackTrace();
            LogUtils.e(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    //更新--牲畜，疫苗
    public int updateLabelVaccine(LabelVaccine labelVaccine) {
        try {
            return labelVaccineDao.update(labelVaccine);
        } catch (SQLException e) {
            e.printStackTrace();
            LogUtils.e(e.getMessage());
        }
        return 0;
    }

    //查询--根据label查询vaccine
    //
    public List<Vaccine> queryVaccineForLabel(Label label) {
        PreparedQuery<Vaccine> vaccineForLabelQuery = null;
        try {
            if (vaccineForLabelQuery == null) {
                vaccineForLabelQuery = makeVaccineForLabelQuery();
            }
            vaccineForLabelQuery.setArgumentHolderValue(0, label);
            Log.e("sql Vaccine", vaccineForLabelQuery.toString());
            return vaccineDao.query(vaccineForLabelQuery);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    //sql示例：SELECT * FROM `Vaccine` WHERE `vaccine_id` IN (SELECT `vaccine_id` FROM `LabelVaccine` WHERE `label_id` = ?)
    public PreparedQuery<Vaccine> makeVaccineForLabelQuery() {
        try {
            QueryBuilder<LabelVaccine, Integer> oostQb = labelVaccineDao.queryBuilder();//内部查询表
            oostQb.selectColumns(LabelVaccine.VACCINE_ID_FIELD_NAME);//查询字段
            SelectArg selectArg = new SelectArg();
            oostQb.where().eq(LabelVaccine.LABEL_ID_FIELD_NAME, selectArg);//查询条件
            QueryBuilder<Vaccine, Integer> postQb = vaccineDao.queryBuilder();//外部查询表
            postQb.where().in(Vaccine.ID_FIELD_NAME, oostQb);//查询条件
            return postQb.prepare();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    //根据vaccine查询LabelVaccine
    public List<LabelVaccine> queryLabelVaccineByVaccine(Vaccine vaccine) {
        try {
            int vaccine_id = vaccine.getVaccine_id();
            return labelVaccineDao.queryBuilder()
                    .where().eq(LabelVaccine.VACCINE_ID_FIELD_NAME, vaccine_id)
                    .query();
        } catch (SQLException e) {
            e.printStackTrace();
            LogUtils.e(e.getMessage());
        }
        return null;
    }

    //查询--根据vaccine查询label
    public List<Label> queryLabelForVaccine(Vaccine vaccine) {
        PreparedQuery<Label> labelForVaccineQuery = null;
        try {
            if (labelForVaccineQuery == null) {
                labelForVaccineQuery = makeLabelForVaccineQuery();
            }
            labelForVaccineQuery.setArgumentHolderValue(0, vaccine);
            return labelDao.query(labelForVaccineQuery);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public PreparedQuery<Label> makeLabelForVaccineQuery() {
        try {
            QueryBuilder<LabelVaccine, Integer> userPostQb = labelVaccineDao.queryBuilder();
            // this time selecting for the user-id field
            userPostQb.selectColumns(LabelVaccine.LABEL_ID_FIELD_NAME);
            SelectArg postSelectArg = new SelectArg();
            userPostQb.where().eq(LabelVaccine.VACCINE_ID_FIELD_NAME, postSelectArg);

            // build our outer query
            QueryBuilder<Label, Integer> userQb = labelDao.queryBuilder();
            // where the user-id matches the inner query's user-id field
            userQb.where().in(Label.ID_FIELD_NAME, userPostQb);
            return userQb.prepare();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

////////////////////////////////////////////////////////////////////////////////////////////////////


    //新增--分群，防治
    public int insertGroupVaccine(GroupVaccine groupVaccine) {
        try {
            return groupVaccineDao.create(groupVaccine);
        } catch (SQLException e) {
            e.printStackTrace();
            LogUtils.e(e.getMessage());
        }
        return 0;
    }

    //新增--分群，防治
    public int insertGroupVaccine(List<GroupVaccine> list) {
        try {
            return groupVaccineDao.create(list);
        } catch (SQLException e) {
            e.printStackTrace();
            LogUtils.e(e.getMessage());
        }
        return 0;
    }

    //删除--分群，防治
    public int deleteGroupVaccine(GroupVaccine groupVaccine) {
        try {
            return groupVaccineDao.delete(groupVaccine);
        } catch (SQLException e) {
            e.printStackTrace();
            LogUtils.e(e.getMessage());
        }
        return 0;
    }

    //批量删除--分群，防治
    public int deleteGroupVaccine(List<GroupVaccine> list) {
        try {
            groupVaccineDao.callBatchTasks(new Callable<Void>() {
                @Override
                public Void call() throws Exception {
                    for (GroupVaccine groupVaccine : list) {
                        groupVaccineDao.delete(groupVaccine);
                    }
                    return null;
                }
            });
            return 0;
        } catch (SQLException e) {
            e.printStackTrace();
            LogUtils.e(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    //更新--分群，防治
    public int updateGroupVaccine(GroupVaccine groupVaccine) {
        try {
            return groupVaccineDao.update(groupVaccine);
        } catch (SQLException e) {
            e.printStackTrace();
            LogUtils.e(e.getMessage());
        }
        return 0;
    }

    //查询--根据label查询vaccine
    public List<Vaccine> queryVaccineForGroup(Group group) {
        PreparedQuery<Vaccine> vaccineForGroupQuery = null;
        try {
            if (vaccineForGroupQuery == null) {
                vaccineForGroupQuery = makeVaccineForGroupQuery();
            }
            vaccineForGroupQuery.setArgumentHolderValue(0, group);
            return vaccineDao.query(vaccineForGroupQuery);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public PreparedQuery<Vaccine> makeVaccineForGroupQuery() {
        try {
            QueryBuilder<GroupVaccine, Integer> userPostQb = groupVaccineDao.queryBuilder();
            userPostQb.selectColumns(GroupVaccine.VACCINE_ID_FIELD_NAME);
            SelectArg userSelectArg = new SelectArg();
            userPostQb.where().eq(GroupVaccine.GROUP_ID_FIELD_NAME, userSelectArg);
            QueryBuilder<Vaccine, Integer> postQb = vaccineDao.queryBuilder();
            postQb.where().in(Vaccine.ID_FIELD_NAME, userPostQb);
            return postQb.prepare();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    //根据vaccine查询GroupVaccine
    public List<GroupVaccine> queryGroupVaccineByVaccine(Vaccine vaccine) {
        try {
            int vaccine_id = vaccine.getVaccine_id();
            return groupVaccineDao.queryBuilder()
                    .where().eq(GroupVaccine.VACCINE_ID_FIELD_NAME, vaccine_id)
                    .query();
        } catch (SQLException e) {
            e.printStackTrace();
            LogUtils.e(e.getMessage());
        }
        return null;
    }

    //查询--根据vaccine查询group
    public List<Group> queryGroupForVaccine(Vaccine vaccine) {
        PreparedQuery<Group> groupForVaccineQuery = null;
        try {
            if (groupForVaccineQuery == null) {
                groupForVaccineQuery = makeGroupForVaccineQuery();
            }
            groupForVaccineQuery.setArgumentHolderValue(0, vaccine);
            return groupDao.query(groupForVaccineQuery);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public PreparedQuery<Group> makeGroupForVaccineQuery() {
        try {
            QueryBuilder<GroupVaccine, Integer> userPostQb = groupVaccineDao.queryBuilder();
            // this time selecting for the user-id field
            userPostQb.selectColumns(GroupVaccine.GROUP_ID_FIELD_NAME);
            SelectArg postSelectArg = new SelectArg();
            userPostQb.where().eq(GroupVaccine.VACCINE_ID_FIELD_NAME, postSelectArg);

            // build our outer query
            QueryBuilder<Group, Integer> userQb = groupDao.queryBuilder();
            // where the user-id matches the inner query's user-id field
            userQb.where().in(Group.ID_FIELD_NAME, userPostQb);
            return userQb.prepare();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

////////////////////////////////////////////////////////////////////////////////////////////////////

    //新增--配种
    public int insertBreed(Breed breed) {
        try {
            return breedDao.create(breed);
        } catch (SQLException e) {
            e.printStackTrace();
            LogUtils.e(e.getMessage());
        }
        return 0;
    }

    //删除--配种
    public int deleteBreed(Breed breed) {
        try {
            return breedDao.delete(breed);
        } catch (SQLException e) {
            e.printStackTrace();
            LogUtils.e(e.getMessage());
        }
        return 0;
    }

    //更新--配种
    public int updateBreed(Breed breed) {
        try {
            return breedDao.update(breed);
        } catch (SQLException e) {
            e.printStackTrace();
            LogUtils.e(e.getMessage());
        }
        return 0;
    }

    //查询--配种
    public List<Breed> queryBreedAll() {
        try {
            return breedDao.queryForAll();
        } catch (SQLException e) {
            e.printStackTrace();
            LogUtils.e(e.getMessage());
        }
        return null;
    }

    //查询--配种
    public Breed queryBreedById(int id) {
        try {
            return breedDao.queryForId(id);
        } catch (SQLException e) {
            e.printStackTrace();
            LogUtils.e(e.getMessage());
        }
        return null;
    }

    //查询--配种--根据名称查询
    public List<Breed> queryBreedByName(String name) {
        try {
            return breedDao.queryBuilder()
                    .where().eq("name", name)
                    .query();
        } catch (SQLException e) {
            e.printStackTrace();
            LogUtils.e(e.getMessage());
        }
        return null;
    }

    //新增--牲畜，配种
    public int insertLabelBreed(LabelBreed labelBreed) {
        try {
            return labelBreedDao.create(labelBreed);
        } catch (SQLException e) {
            e.printStackTrace();
            LogUtils.e(e.getMessage());
        }
        return 0;
    }

    //新增--牲畜，配种
    public int insertLabelBreed(List<LabelBreed> list) {
        try {
            return labelBreedDao.create(list);
        } catch (SQLException e) {
            e.printStackTrace();
            LogUtils.e(e.getMessage());
        }
        return 0;
    }

    //删除--牲畜，配种
    public int deleteLabelBreed(LabelBreed labelBreed) {
        try {
            return labelBreedDao.delete(labelBreed);
        } catch (SQLException e) {
            e.printStackTrace();
            LogUtils.e(e.getMessage());
        }
        return 0;
    }

    //批量删除--牲畜，配种
    public int deleteLabelBreed(List<LabelBreed> list) {
        try {
            labelGroupDao.callBatchTasks(new Callable<Void>() {
                @Override
                public Void call() throws Exception {
                    for (LabelBreed labelBreed : list) {
                        labelBreedDao.delete(labelBreed);
                    }
                    return null;
                }
            });
            return 0;
        } catch (SQLException e) {
            e.printStackTrace();
            LogUtils.e(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    //根据breed查询LabelBreed
    public List<LabelBreed> queryLabelBreedByBreed(Breed breed) {
        try {
            int breed_id = breed.getBreed_id();
            return labelBreedDao.queryBuilder()
                    .where().eq(LabelBreed.BREED_ID_FIELD_NAME, breed_id)
                    .query();
        } catch (SQLException e) {
            e.printStackTrace();
            LogUtils.e(e.getMessage());
        }
        return null;
    }

    //查询--根据breed查询label
    public List<Label> queryLabelForBreed(Breed breed, String id) {
        PreparedQuery<Label> labelForBreedQuery = null;
        try {
            if (labelForBreedQuery == null) {
                labelForBreedQuery = makeLabelForBreedQuery(id);
                Log.e("sql", labelForBreedQuery.toString());
            }
            labelForBreedQuery.setArgumentHolderValue(0, breed);
            Log.e("sql", labelForBreedQuery.toString());
            return labelDao.query(labelForBreedQuery);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public PreparedQuery<Label> makeLabelForBreedQuery(String id) {
        try {
            QueryBuilder<LabelBreed, Integer> userPostQb = labelBreedDao.queryBuilder();
            // this time selecting for the user-id field
            userPostQb.selectColumns(id);
            SelectArg postSelectArg = new SelectArg();
            userPostQb.where().eq(LabelBreed.BREED_ID_FIELD_NAME, postSelectArg);

            // build our outer query
            QueryBuilder<Label, Integer> userQb = labelDao.queryBuilder();
            // where the user-id matches the inner query's user-id field
            userQb.where().in(Label.ID_FIELD_NAME, userPostQb);
            return userQb.prepare();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    //查询--根据label查询breed
    public List<Breed> queryBreedForLabel(Label label) {
        PreparedQuery<Breed> breedForLabelQuery = null;
        try {
            if (breedForLabelQuery == null) {
                breedForLabelQuery = makeBreedForLabelQuery();
            }
            breedForLabelQuery.setArgumentHolderValue(0, label);
            breedForLabelQuery.setArgumentHolderValue(1, label);
            Log.e("sql Breed", breedForLabelQuery.toString());
            return breedDao.query(breedForLabelQuery);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    // select * from Breed where breed_id in (select breed_id from LabelBreed where label_m_id = ? or label_w_id = ?)
    public PreparedQuery<Breed> makeBreedForLabelQuery() {
        try {
            QueryBuilder<LabelBreed, Integer> postQb = labelBreedDao.queryBuilder();
            // this time selecting for the user-id field
            postQb.selectColumns(LabelBreed.BREED_ID_FIELD_NAME);
            SelectArg selectArg = new SelectArg();
            postQb.where().eq(LabelBreed.LABEL_ID_M_NAME, selectArg)
                    .or().eq(LabelBreed.LABEL_ID_W_NAME, new SelectArg());

            // build our outer query
            QueryBuilder<Breed, Integer> userQb = breedDao.queryBuilder();
            // where the user-id matches the inner query's user-id field
            userQb.where().in(Breed.ID_FIELD_NAME, postQb);
            return userQb.prepare();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }


//    //查询--根据label查询breed
//    public List<Breed> queryBreedForLabel(Label label) {
//        PreparedQuery<Breed> breedForLabelQuery = null;
//        try {
//            if (breedForLabelQuery == null) {
//                breedForLabelQuery = makeBreedForLabelQuery();
//            }
//            breedForLabelQuery.setArgumentHolderValue(0, label);
//            return breedDao.query(breedForLabelQuery);
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
//        return null;
//    }
//
//    public PreparedQuery<Breed> makeBreedForLabelQuery() {
//        try {
//            QueryBuilder<LabelBreed, Integer> userPostQb = labelBreedDao.queryBuilder();
//            userPostQb.selectColumns(LabelBreed.BREED_ID_FIELD_NAME);
//            SelectArg userSelectArg = new SelectArg();
//            userPostQb.where().eq(LabelBreed.LABEL_ID_W_NAME, userSelectArg).or()
//                    .eq(LabelBreed.LABEL_ID_M_NAME, userSelectArg);
//            QueryBuilder<Breed, Integer> postQb = breedDao.queryBuilder();
//            postQb.where().in(Breed.ID_FIELD_NAME, userPostQb);
//            return postQb.prepare();
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
//        return null;
//    }

////////////////////////////////////////////////////////////////////////////////////////////////////

    //新增--饲料
    public int insertFood(Food food) {
        try {
            return foodDao.create(food);
        } catch (SQLException e) {
            e.printStackTrace();
            LogUtils.e(e.getMessage());
        }
        return 0;
    }

    //删除--饲料
    public int deleteFood(Food food) {
        try {
            return foodDao.delete(food);
        } catch (SQLException e) {
            e.printStackTrace();
            LogUtils.e(e.getMessage());
        }
        return 0;
    }

    //更新--饲料
    public int updateFood(Food food) {
        try {
            return foodDao.update(food);
        } catch (SQLException e) {
            e.printStackTrace();
            LogUtils.e(e.getMessage());
        }
        return 0;
    }

    //查询--饲料
    public List<Food> queryFoodAll() {
        try {
            return foodDao.queryForAll();
        } catch (SQLException e) {
            e.printStackTrace();
            LogUtils.e(e.getMessage());
        }
        return null;
    }

    //查询--饲料
    public Food queryFoodById(int id) {
        try {
            return foodDao.queryForId(id);
        } catch (SQLException e) {
            e.printStackTrace();
            LogUtils.e(e.getMessage());
        }
        return null;
    }

    //查询--饲料（根据名称查询）
    public List<Food> queryFoodByName(String name) {
        try {
            return foodDao.queryBuilder()
                    .where().eq("name", name)
                    .query();
        } catch (SQLException e) {
            e.printStackTrace();
            LogUtils.e(e.getMessage());
        }
        return null;
    }

////////////////////////////////////////////////////////////////////////////////////////////////////

    //新增--饲喂
    public int insertFeed(Feed feed) {
        try {
            return feedDao.create(feed);
        } catch (SQLException e) {
            e.printStackTrace();
            LogUtils.e(e.getMessage());
        }
        return 0;
    }

    //删除--饲喂
    public int deleteFeed(Feed feed) {
        try {
            return feedDao.delete(feed);
        } catch (SQLException e) {
            e.printStackTrace();
            LogUtils.e(e.getMessage());
        }
        return 0;
    }

    //更新--饲喂
    public int updateFeed(Feed feed) {
        try {
            return feedDao.update(feed);
        } catch (SQLException e) {
            e.printStackTrace();
            LogUtils.e(e.getMessage());
        }
        return 0;
    }

    //查询--饲喂
    public List<Feed> queryFeedAll() {
        try {
            return feedDao.queryForAll();
        } catch (SQLException e) {
            e.printStackTrace();
            LogUtils.e(e.getMessage());
        }
        return null;
    }

    //查询--饲喂
    public Feed queryFeedById(int id) {
        try {
            return feedDao.queryForId(id);
        } catch (SQLException e) {
            e.printStackTrace();
            LogUtils.e(e.getMessage());
        }
        return null;
    }


    //新增--饲喂、分群、饲料
    public int insertFeedGroupFood(FeedGroupFood f) {
        try {
            return feedGroupFoodDao.create(f);
        } catch (SQLException e) {
            e.printStackTrace();
            LogUtils.e(e.getMessage());
        }
        return 0;
    }

    //新增--饲喂、分群、饲料
    public int insertFeedGroupFood(List<FeedGroupFood> list) {
        try {
            return feedGroupFoodDao.create(list);
        } catch (SQLException e) {
            e.printStackTrace();
            LogUtils.e(e.getMessage());
        }
        return 0;
    }

    //删除--饲喂、分群、饲料
    public int deleteFeedGroupFood(FeedGroupFood f) {
        try {
            return feedGroupFoodDao.delete(f);
        } catch (SQLException e) {
            e.printStackTrace();
            LogUtils.e(e.getMessage());
        }
        return 0;
    }

    //批量删除--饲喂、分群、饲料
    public int deleteFeedGroupFood(List<FeedGroupFood> list) {
        try {
            labelGroupDao.callBatchTasks(new Callable<Void>() {
                @Override
                public Void call() throws Exception {
                    for (FeedGroupFood f : list) {
                        feedGroupFoodDao.delete(f);
                    }
                    return null;
                }
            });
            return 0;
        } catch (SQLException e) {
            e.printStackTrace();
            LogUtils.e(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    //根据food查询FeedGroupFood
    public List<FeedGroupFood> queryFeedGroupFoodByFood(Food food) {
        try {
            int food_id = food.getFood_id();
            return feedGroupFoodDao.queryBuilder()
                    .where().eq(FeedGroupFood.FOOD_ID_FIELD_NAME, food_id)
                    .query();
        } catch (SQLException e) {
            e.printStackTrace();
            LogUtils.e(e.getMessage());
        }
        return null;
    }

    //根据feed查询FeedGroupFood
    public List<FeedGroupFood> queryFeedGroupFoodByFeed(Feed feed) {
        try {
            int feed_id = feed.getFeed_id();
            return feedGroupFoodDao.queryBuilder()
                    .where().eq(FeedGroupFood.FEED_ID_FIELD_NAME, feed_id)
                    .query();
        } catch (SQLException e) {
            e.printStackTrace();
            LogUtils.e(e.getMessage());
        }
        return null;
    }

    //查询--根据feed查询group
    public List<Group> queryGroupForFeed(Feed feed, String id) {
        PreparedQuery<Group> groupForFeedQuery = null;
        try {
            if (groupForFeedQuery == null) {
                groupForFeedQuery = makeGroupForFeedQuery(id);
            }
            groupForFeedQuery.setArgumentHolderValue(0, feed);
            return groupDao.query(groupForFeedQuery);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public PreparedQuery<Group> makeGroupForFeedQuery(String id) {
        try {
            QueryBuilder<FeedGroupFood, Integer> qb = feedGroupFoodDao.queryBuilder();
            qb.selectColumns(id);
            SelectArg postSelectArg = new SelectArg();
            qb.where().eq(FeedGroupFood.FEED_ID_FIELD_NAME, postSelectArg);
            QueryBuilder<Group, Integer> groupQb = groupDao.queryBuilder();
            groupQb.where().in(Group.ID_FIELD_NAME, qb);
            return groupQb.prepare();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    //查询--根据feed查询food
    public List<Food> queryFoodForFeed(Feed feed, String id) {
        PreparedQuery<Food> foodForFeedQuery = null;
        try {
            if (foodForFeedQuery == null) {
                foodForFeedQuery = makeFoodForFeedQuery(id);
            }
            foodForFeedQuery.setArgumentHolderValue(0, feed);
            return foodDao.query(foodForFeedQuery);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public PreparedQuery<Food> makeFoodForFeedQuery(String id) {
        try {
            QueryBuilder<FeedGroupFood, Integer> qb = feedGroupFoodDao.queryBuilder();
            qb.selectColumns(id);
            SelectArg postSelectArg = new SelectArg();
            qb.where().eq(FeedGroupFood.FEED_ID_FIELD_NAME, postSelectArg);
            QueryBuilder<Food, Integer> foodQb = foodDao.queryBuilder();
            foodQb.where().in(Food.ID_FIELD_NAME, qb);
            return foodQb.prepare();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }


    //
//    //查询所有
//    @Override
//    public ArrayList<Label> queryAll() {
//        try {
//            return (ArrayList<Label>) labelDao.queryForAll();
//        } catch (SQLException e) {
//            e.printStackTrace();
//            LogUtil.e(e.getMessage());
//        }
//        return null;
//    }
//
//    @Override
//    public ArrayList<Label> queryByCodeAndWj(String code, String wj, String zip) {
//        try {
//            return (ArrayList<Label>) labelDao.queryBuilder()
//                    .where().eq("code", code)
//                    .and().eq("wj", wj)
//                    .and().eq("zip", zip).query();
//        } catch (SQLException e) {
//            e.printStackTrace();
//            LogUtil.e(e.getMessage());
//        }
//        return null;
//    }
//
//    //根据值查询
//    @Override
//    public ArrayList<Label> queryByName(String name) {
//        try {
//            return (ArrayList<Label>) labelDao.queryBuilder()
//                    .where().eq("name", name).query();
//        } catch (SQLException e) {
//            e.printStackTrace();
//            LogUtil.e(e.getMessage());
//        }
//        return null;
//    }
//
//    //根据文件名和zip名查询
//    @Override
//    public ArrayList<Label> queryByZipWj(String wj, String zip) {
//        try {
//            return (ArrayList<Label>) labelDao.queryBuilder()
//                    .where().eq("wj", wj)
//                    .and().eq("zip", zip).query();
//        } catch (SQLException e) {
//            e.printStackTrace();
//            LogUtil.e(e.getMessage());
//        }
//        return null;
//    }
//
//    //根据文件名和zip名查询
//    @Override
//    public List<Label> queryByZipWj2(String name, String wj, String zip) {
//        try {
//            //select distinct name from pd where wj = wj and zip = zip;
//            return labelDao.queryBuilder().selectColumns(name).distinct()
//                    .where().eq("wj", wj)
//                    .and().eq("zip", zip).query();
//        } catch (SQLException e) {
//            e.printStackTrace();
//            LogUtil.e(e.getMessage());
//        }
//        return null;
//    }
//
////    //根据文件名查询
////    @Override
////    public ArrayList<T_PD> query(String wj, String zip) {
////        try {
////            return (ArrayList<T_PD>) t_pdDao.queryBuilder()
////                    .where().eq("wj", wj)
////                    .and().eq("zip", zip).query();
////        } catch (SQLException e) {
////            e.printStackTrace();
////            LogUtil.e(e.getMessage());
////        }
////        return null;
////    }
//
//    @Override
//    public ArrayList<T_PD> queryByGKAndJG(String gk, String jg, String zip) {
//        try {
//            if (gk.equals("")) {
//                return (ArrayList<T_PD>) labelDao.queryBuilder()
//                        .where().eq("zxpdjg", jg)
//                        .and().ne("result", "")//不等于
//                        .and().eq("zip", zip).query();
//            }
//            if (jg.equals("")) {
//                return (ArrayList<T_PD>) labelDao.queryBuilder()
//                        .where().eq("gkglbm", gk)
//                        .and().ne("result", "")//不等于
//                        .and().eq("zip", zip).query();
//            }
//            return (ArrayList<T_PD>) labelDao.queryBuilder()
//                    .where().eq("gkglbm", gk)
//                    .and().eq("zxpdjg", jg)
//                    .and().ne("result", "")//不等于
//                    .and().eq("zip", zip).query();
//        } catch (SQLException e) {
//            e.printStackTrace();
//            LogUtil.e(e.getMessage());
//        }
//        return null;
//    }
//
//    //查询归口机构名称
//    @Override
//    public ArrayList<T_PD> queryGKAndJG(String wj, String zip) {
//        try {
//            return (ArrayList<T_PD>) labelDao.queryBuilder()
//                    .where().eq("wj", wj)
//                    .and().eq("zip", zip)
//                    .and().ne("result", "")
//                    .and().ne("result", "盘盈").query();
//        } catch (SQLException e) {
//            e.printStackTrace();
//            LogUtil.e(e.getMessage());
//        }
//        return null;
//    }
//
//    //根据zip文件名查询总资产
//    @Override
//    public ArrayList<T_PD> queryAllAssets(String wj, String zip) {
//        try {
//            return (ArrayList<T_PD>) labelDao.queryBuilder()
//                    .where().eq("wj", wj)
//                    .and().eq("zip", zip).query();
//        } catch (SQLException e) {
//            e.printStackTrace();
//            LogUtil.e(e.getMessage());
//        }
//        return null;
//    }
//
//    //根据zip文件名、wj和RFID查询总资产下的单个资产
//    @Override
//    public ArrayList<T_PD> queryAssetsByZipWjCode(String wj, String zip, String code) {
//        try {
//            return (ArrayList<T_PD>) labelDao.queryBuilder()
//                    .where().eq("wj", wj)
//                    .and().eq("zip", zip)
//                    .and().eq("code", code).query();
//        } catch (SQLException e) {
//            e.printStackTrace();
//            LogUtil.e(e.getMessage());
//        }
//        return null;
//    }
//
//    //根据zip文件名和RFID查询盘点单下的单个资产
//    @Override
//    public ArrayList<T_PD> queryAssetsByZipCode(String zip, String code) {
//        try {
//            return (ArrayList<T_PD>) labelDao.queryBuilder()
//                    .where().eq("zip", zip)
//                    .and().eq("code", code).query();
//        } catch (SQLException e) {
//            e.printStackTrace();
//            LogUtil.e(e.getMessage());
//        }
//        return null;
//    }
//
//    //更新备注
//    @Override
//    public int updateRemark(T_PD t_pd) {
//        try {
//            UpdateBuilder<T_PD, Integer> builder = labelDao.updateBuilder();
//            builder.setWhere(builder.where().eq("code", t_pd.getCcode())
//                    .and().eq("wj", t_pd.getWj())
//                    .and().eq("zip", t_pd.getZip())
//                    .and().ne("result", ""));
//            builder.updateColumnValue("remark", t_pd.getLremark());
//            return builder.update();
//        } catch (SQLException e) {
//            e.printStackTrace();
//            LogUtil.e(e.getMessage());
//        }
//        return 0;
//    }
//
//    //更新数据
//    @Override
//    public int update(T_PD t_pd) {
//        try {
//            return labelDao.update(t_pd);
//        } catch (SQLException e) {
//            e.printStackTrace();
//            LogUtil.e(e.getMessage());
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return 0;
//    }
//
//    //更新盘点数据
//    @Override
//    public int updateInventory(T_PD t_pd) {
//        try {
//            UpdateBuilder<T_PD, Integer> builder = labelDao.updateBuilder();
//            builder.setWhere(builder.where().eq("code", t_pd.getCcode())
//                    .and().eq("wj", t_pd.getWj())
//                    .and().eq("zip", t_pd.getZip()));
//            builder.updateColumnValue("result", t_pd.getGresult());
//            builder.updateColumnValue("way", t_pd.getWay());
//            builder.updateColumnValue("operate", t_pd.getLoperate());//盘点人
//            return builder.update();
//        } catch (SQLException e) {
//            e.printStackTrace();
//            LogUtil.e(e.getMessage());
//        }
//        return 0;
//    }
//
//    //批量更新盘点数据
//    @Override
//    public int updateInventoryBatch(ArrayList<T_PD> t_pds) {
//        try {
//            labelDao.callBatchTasks(new Callable<Void>() {
//                @Override
//                public Void call() throws Exception {
//                    for (T_PD t_pd : t_pds) {
//                        int update = labelDao.update(t_pd);
//                    }
//                    return null;
//                }
//            });
//            return 0;
//        } catch (SQLException e) {
//            e.printStackTrace();
//            LogUtil.e(e.getMessage());
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return 0;
//    }
//
//    //删除盘盈
//    @Override
//    public int deletePanYing(T_PD t_pd) {
//        try {
//            DeleteBuilder<T_PD, Integer> builder = labelDao.deleteBuilder();
//            builder.setWhere(builder.where().eq("result", t_pd.getGresult())
//                    .and().eq("wj", t_pd.getWj())
//                    .and().eq("zip", t_pd.getZip()));
//            return builder.delete();
//        } catch (SQLException e) {
//            e.printStackTrace();
//            LogUtil.e(e.getMessage());
//        }
//        return 0;
//    }
//
//    //批量删除盘盈
//    @Override
//    public int deletePanYingBatch(ArrayList<T_PD> t_pds) {
//        try {
//            labelDao.callBatchTasks(new Callable<Void>() {
//                @Override
//                public Void call() throws Exception {
//                    for (T_PD t_pd : t_pds) {
//                        DeleteBuilder<T_PD, Integer> builder = labelDao.deleteBuilder();
//                        builder.setWhere(builder.where().eq("code", t_pd.getCcode())
//                                .and().eq("result", App.PANYING)
//                                .and().eq("zip", t_pd.getZip()));
//                        builder.delete();
//                    }
//                    return null;
//                }
//            });
//            return 0;
//        } catch (SQLException e) {
//            e.printStackTrace();
//            LogUtil.e(e.getMessage());
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return 0;
//    }
//
//    //删除单个盘盈
//    @Override
//    public int deletePanYingOne(T_PD t_pd) {
//        try {
//            DeleteBuilder<T_PD, Integer> builder = labelDao.deleteBuilder();
//            builder.setWhere(builder.where().eq("result", t_pd.getGresult())
//                    .and().eq("code", t_pd.getCcode())
//                    .and().eq("wj", t_pd.getWj())
//                    .and().eq("zip", t_pd.getZip()));
//            return builder.delete();
//        } catch (SQLException e) {
//            e.printStackTrace();
//            LogUtil.e(e.getMessage());
//        }
//        return 0;
//    }
//
//    //删除单个盘点单数据
//    @Override
//    public int deleteByZip(String zip) {
//        try {
//            DeleteBuilder<T_PD, Integer> builder = labelDao.deleteBuilder();
//            builder.setWhere(builder.where().eq("zip", zip));
//            return builder.delete();
//        } catch (SQLException e) {
//            e.printStackTrace();
//            LogUtil.e(e.getMessage());
//        }
//        return 0;
//    }
//
//    //删除所有
//    @Override
//    public int deleteAll() {
//        try {
//            String sql = "delete from T_PD";
//            GenericRawResults<String[]> delete_from_t_pd = labelDao.queryRaw(sql);
//            return delete_from_t_pd.getResults().size();
//        } catch (SQLException e) {
//            e.printStackTrace();
//            LogUtil.e(e.getMessage());
//        }
//        return 0;
//    }
}
